package
{
	import cn._2dland.comment.CommentMC;
	import cn._2dland.comment.CommentView;
	import cn._2dland.events.VideoEvent;
	import cn._2dland.net.NetConnectionFactory;
	import cn._2dland.net.VideoInfoLoader;
	import cn._2dland.ui.PlayerSlider;
	import cn._2dland.utils.CommentUtils;
	import cn._2dland.utils.VideoUtils;
	import cn._2dland.video.VideoComponent;
	
	import fl.controls.Button;
	import fl.controls.CheckBox;
	import fl.controls.ComboBox;
	import fl.controls.Label;
	import fl.controls.ProgressBar;
	import fl.controls.ProgressBarMode;
	import fl.controls.Slider;
	import fl.data.DataProvider;
	import fl.events.SliderEvent;
	import fl.managers.StyleManager;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.system.System;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;

	[SWF(width=540,height=432,fps=30)]

	/**
	 * AS版播放器
	 * @author deadblue
	 * 
	 */	
	public class ASPlayer extends Sprite
	{
		private var _videoLoader:VideoInfoLoader = null;

		private var _videoComp:VideoComponent = null;
		private var _playLabel:Label = null;
		private var _totalLabel:Label = null;
		private var _playSlider:Slider = null;
		private var _playBtn:Button = null;
		private var _partList:ComboBox = null;
		private var _rptCheckBox:CheckBox = null;

		private var _isDraging:Boolean = false;

		public function ASPlayer() {
			StyleManager.setStyle("textFormat", new TextFormat("Verdana", 12, 0, true));
			StyleManager.setStyle("disabledTextFormat", new TextFormat("Verdana", 12, 0x999999, true));
			tabChildren = false;
			initUI();

			_videoLoader = new VideoInfoLoader();
			_videoLoader.addEventListener(Event.COMPLETE, loadCompleteHandler);
			_videoLoader.load(loaderInfo.parameters.dir, loaderInfo.parameters.vid, true);
		}

		private function initUI():void {
			_videoComp = new VideoComponent();
			_videoComp.addEventListener(VideoEvent.READY, videoReadyHandler);
			_videoComp.addEventListener(VideoEvent.PLAY, videoPlayHandler);
			_videoComp.addEventListener(VideoEvent.INFO_UPDATE, videoInfoUpdateHandelr);
			_videoComp.addEventListener(VideoEvent.PAUSE, videoPauseHandler);
			_videoComp.addEventListener(VideoEvent.STOP, videoStopHandler);
			_videoComp.addEventListener(VideoEvent.PLAYING, videoPlayingHandler);
			_videoComp.addEventListener(VideoEvent.CHANGE_PART, videoChangePartHandler);
			_videoComp.addEventListener(VideoEvent.SEEK, videoSeekHandler);
			addChild(_videoComp);
			_videoComp.move(0, 0);

			var tf:TextFormat = new TextFormat("Verdana", 12, 0, true, false, false, null, null, TextFormatAlign.CENTER);
			tf.align = TextFormatAlign.CENTER;

			_playLabel = new Label();
			_playLabel.width = 60;
			_playLabel.setStyle("textFormat", tf);
			_playLabel.text = "00:00";
			addChild(_playLabel);
			_playLabel.move(0, 388);

			_totalLabel = new Label();
			_totalLabel.width = 60;
			_totalLabel.setStyle("textFormat", tf);
			_totalLabel.text = "00:00";
			addChild(_totalLabel);
			_totalLabel.move(480, 388);

			var progBar:ProgressBar = new ProgressBar();
			progBar.width = 420;
			progBar.height = 16;
			progBar.mode = ProgressBarMode.EVENT;
			progBar.source = _videoComp;
			addChild(progBar);
			progBar.move(60, 388);

			_playSlider = new PlayerSlider();
			_playSlider.width = 420;
			_playSlider.height = 16;
			_playSlider.liveDragging = false;
			_playSlider.addEventListener(SliderEvent.CHANGE, sliderChangeHandler);
			_playSlider.addEventListener(SliderEvent.THUMB_PRESS, thumbPressHandler);
			_playSlider.addEventListener(SliderEvent.THUMB_RELEASE, thumbReleaseHandler);
			addChild(_playSlider);
			_playSlider.move(60, 388);

			_playBtn = new Button();
			_playBtn.label = "＞";
			_playBtn.width = 32;
			_playBtn.enabled = false;
			_playBtn.addEventListener(MouseEvent.CLICK, playButtonHandler);
			addChild(_playBtn);
			_playBtn.move(0, 408);

			_partList = new ComboBox();
			_partList.width = 200;
			_partList.dropdown.alpha = 0.7;
			_partList.labelField = "name";
			_partList.addEventListener(Event.CHANGE, partChangeHandler);
			addChild(_partList);
			_partList.move(276, 408);

			_rptCheckBox = new CheckBox();
			_rptCheckBox.tabEnabled = false;
			_rptCheckBox.label = "循环";
			addChild(_rptCheckBox);
			_rptCheckBox.move(480, 408);
		}

		protected function loadCompleteHandler(e:Event):void {
			_videoComp.load(_videoLoader.infos);
		}

		protected function videoReadyHandler(e:VideoEvent):void {
			_playBtn.enabled = true;
			_totalLabel.text = VideoUtils.formatMillisecond(_videoComp.duration);
			_playSlider.maximum = _videoComp.duration;
			_partList.dataProvider = new DataProvider(_videoComp.parts);
		}

		protected function videoInfoUpdateHandelr(e:VideoEvent):void {
			_totalLabel.text = VideoUtils.formatMillisecond(_videoComp.duration);
			_playSlider.maximum = _videoComp.duration;
		}

		protected function videoPlayHandler(e:VideoEvent):void {
			_playBtn.label = "Ⅱ";
		}

		protected function videoPauseHandler(e:VideoEvent):void {
			_playBtn.label = "＞";
		}

		protected function videoStopHandler(e:VideoEvent):void {
			if(_rptCheckBox.selected)
				_videoComp.play();
		}

		protected function videoPlayingHandler(e:VideoEvent):void {
			if(!_isDraging){
				_playSlider.value = e.playhead;
				_playLabel.text = VideoUtils.formatMillisecond(e.playhead);
			}
		}

		protected function videoSeekHandler(e:VideoEvent):void {
			_playSlider.value = e.playhead;
			_playLabel.text = VideoUtils.formatMillisecond(e.playhead);
		}

		protected function videoChangePartHandler(e:VideoEvent):void {
			_partList.selectedIndex = e.partIndex;
		}

		protected function sliderChangeHandler(e:SliderEvent):void {
			_videoComp.seek(e.value);
		}

		protected function thumbPressHandler(e:SliderEvent):void {
			_isDraging = true;
		}

		protected function thumbReleaseHandler(e:SliderEvent):void {
			_isDraging = false;
		}

		protected function playButtonHandler(e:MouseEvent):void {
			if(_videoComp.isPlaying)
				_videoComp.pause();
			else
				_videoComp.play();
		}

		protected function partChangeHandler(e:Event):void {
			_videoComp.jump(_partList.selectedItem.id);
		}
	}
}